﻿Imports SCIEF.ActiveFramework.CommonExt.Base.IdRepository
Namespace PL.MESSDK
    Public Class SpecificationRevision_PL
        Inherits PLBase
        Implements IPLProcess

        Public WithEvents childMetaData As MetaDataTable

        Public Property SpecificationId_FK As Guid?

        Private _aQueryName As String
        Public aPLSpecTestRev As PLBase


        Public Sub New()
            MyBase.New()
        End Sub


        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
            _aQueryName = queryName

        End Sub

        Public Overrides Function InitData(Optional withChild As Boolean = True) As Boolean
            Try
                If MyBase.InitData(withChild) Then
                    aPLSpecTestRev = PLBase.CreateInstanceOf("SpecificationTestRevision_PL")
                    aPLSpecTestRev.MetaDataTable = DirectCast(Me.Dataset.Tables("SpecificationTestRevision"), MetaDataTable)
                End If
                Return True

            Catch ex As Exception
                Return False
            End Try

        End Function



        Public Overrides Function Save() As Boolean



            Return MyBase.Save


        End Function

        Public Overridable Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating

        End Sub

        Public Overridable Sub OnRowUpdatingChild(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles childMetaData.OnRowUpdating

        End Sub


        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Public Function BatchRecords(dataRow() As System.Data.DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            If SpecificationId_FK IsNot Nothing Then
                dataRow("SpecificationId_FK") = SpecificationId_FK
            End If

            'Default values = 1, so can edit the Attibute and Test values
            dataRow("AttributeEnabled") = 1
            dataRow("TestEnabled") = 1

            dataRow("SpecificationAttributeId") = Guid.NewGuid
            Return True
        End Function

        Public Shared Function CopyToNewSpecRevision(ByVal specificationRevisionID As Guid, ByVal specificationID As Guid) As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_SpecificationRevision_Copy")

                sqlcmd.Parameters("@SpecificationRevId").Value = specificationRevisionID
                sqlcmd.Parameters("@SpecificationId").Value = specificationID

                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function
        ''' <summary>
        ''' This method creates a New Specification Revision and Test are enabled for creation
        ''' </summary>
        ''' <param name="specificationID"></param>
        ''' <returns></returns>
        ''' <remarks>Used bsp:bsp_SpecificationRevision_Create
        ''' Used Query : bsp_SpecificationRevision_Create </remarks>
        Public Shared Function CreateNewSpecRevision(ByVal specificationID As Guid) As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            'tested BD 2014-03-24
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_SpecificationRevision_Create")

                sqlcmd.Parameters("@SpecificationRevId").Value = Nothing
                sqlcmd.Parameters("@SpecificationId_FK").Value = specificationID
                sqlcmd.Parameters("@RevisionNumber").Value = Nothing
                sqlcmd.Parameters("@RevisionDate").Value = Date.Today
                sqlcmd.Parameters("@IsActivated").Value = 0
                sqlcmd.Parameters("@Comment").Value = Nothing
                sqlcmd.Parameters("@ActivatedDate").Value = Nothing
                sqlcmd.Parameters("@TestEnabled").Value = 1

                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function

        Public Shared Function Activate(ByVal specificationRevisionID As Guid) As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaID(IdRepository.MQ_00379_bsp_SpecificationRevision_Activate)
                sqlcmd.Parameters("@SpecificationRevId").Value = specificationRevisionID
                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function


        Public Shared Function UnActivate(ByVal specificationRevisionID As Guid) As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaID(IdRepository.MQ_00379_bsp_SpecificationRevision_UnActivate)
                sqlcmd.Parameters("@SpecificationRevId").Value = specificationRevisionID
                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function



        Public Shared Function EnableTest(ByVal specificationRevisionID As Guid) As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_SpecificationRevision_EnableTest")
                sqlcmd.Parameters("@SpecificationRevId").Value = specificationRevisionID
                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function


        Public Shared Function EnableAttribute(ByVal specificationRevisionID As Guid) As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_SpecificationRevision_EnableAttribute")
                sqlcmd.Parameters("@SpecificationRevId").Value = specificationRevisionID
                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function
    End Class
End Namespace
